﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections;
using Cmd.TypeConverters;

namespace Cmd
{
    public sealed class CommandLineOption
    {
        public string Name { get; internal set; }
        public string Command { get; internal set; }
        public string Alias { get; internal set; }
        public string PropertyName { get; internal set; }
        public string Description { get; internal set; }
        public bool Required { get; internal set; }
        public bool IsDefined { get; internal set; }
        public object Value { get; internal set; }
        public Type Type { get; internal set; }
        internal bool IsHelp { get; set; }
        public UniqueCollection<string> Groups { get; private set; }

        public CommandLineOption(string command)
        {
            Command = command;
            Groups = new UniqueCollection<string>();
        }

        public override string ToString()
        {
            return string.Format("{0}{1}, Value = {2}", Command, string.IsNullOrWhiteSpace(Alias) ? "" : string.Format(" ({0})", Alias), GetValue(true));
        }

        public string ToString(bool asArgument)
        {
            if (asArgument)
            {
                string value = GetValue(false);
                if (value == null)
                {
                    return string.Empty;
                }
                if (value == string.Empty)
                {
                    return string.Format("{0}=\"\"", Command);
                }
                if (value.IndexOf(" ") >= 0)
                {
                    return string.Format("{0}=\"{1}\"", Command, value);
                }
                return string.Format("{0}={1}", Command, value);
            }
            return ToString();
        }

        private string GetValue(bool showNull = false)
        {
            if (this.Value == null && showNull)
            {
                return "null";
            }
            return this.Value.ToString(Type);
        }
    }
}
